package com.h3c.nem.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.enums.OfferWorkerStatusEnum;
import com.h3c.nem.service.ITbOfferWorkerService;
import com.h3c.nem.service.TbOfferWorkerHisService;
import com.h3c.nem.utils.BusiEnumDefine;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author chenyifan
 * @date 2022/7/1 16:33
 * 超过计划入职时间6个月取消入职
 */
@Slf4j
@JobHandler(value = "CancellationOfferWorker")
@Component
@AllArgsConstructor
public class CancellationOfferWorker extends IJobHandler {

    private final ITbOfferWorkerService tbOfferWorkerService;
    private final TbOfferWorkerHisService tbOfferWorkerHisService;

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        XxlJobLogger.log("XXL-JOB, CancellationOfferWorker启动");
        try {
            List<TbOfferWorker> cancelPersons = tbOfferWorkerService.list(new QueryWrapper<TbOfferWorker>().lambda()
                    .in(TbOfferWorker::getWorkerStatus, Arrays.asList(
                            OfferWorkerStatusEnum.待发放offer,
                            OfferWorkerStatusEnum.待填写入职资料,
                            OfferWorkerStatusEnum.待补充资料,
                            OfferWorkerStatusEnum.待审核入职资料,
                            OfferWorkerStatusEnum.待签合同))
                    .le(TbOfferWorker::getPlanReportDate, DateUtil.offsetMonth(new Date(), -6))
            );
            XxlJobLogger.log("XXL-JOB, CancellationOfferWorker, 人数：{}", cancelPersons.size());
            for (TbOfferWorker cancelPerson : cancelPersons) {
                log.info("预备员工"+cancelPerson.getWorkerId()+"因超过计划入职时间6个月未入职，正在取消入职");
                try {
                    tbOfferWorkerService.approvalOfferWorkerData(cancelPerson.getWorkerId(), "超过计划入职时间6个月，自动取消入职",
                            4, "", BusiEnumDefine.NOTICE_MAIL_SEND_NO);
                    tbOfferWorkerHisService.moveWorkerToHisSingleForCancel(cancelPerson);
                } catch (Exception e) {
                    XxlJobLogger.log("预备员工"+cancelPerson.getWorkerId()+"失败:"+e.getMessage());
                    log.error("预备员工"+cancelPerson.getWorkerId()+"失败:"+e.getMessage());
                }
            }
        } catch (Exception e) {
            XxlJobLogger.log("XXL-JOB, CancellationOfferWorker失败:" + e.getMessage());
            return ReturnT.FAIL;
        }
        XxlJobLogger.log("XXL-JOB, CancellationOfferWorker结束");
        return SUCCESS;
    }

    public void cancelOfferWorkerTest() {
        log.info("XXL-JOB, CancellationOfferWorker启动test");
        try {
            List<TbOfferWorker> cancelPersons = tbOfferWorkerService.list(new QueryWrapper<TbOfferWorker>().lambda()
                    .in(TbOfferWorker::getWorkerStatus, Arrays.asList(
                            OfferWorkerStatusEnum.待发放offer,
                            OfferWorkerStatusEnum.待填写入职资料,
                            OfferWorkerStatusEnum.待补充资料,
                            OfferWorkerStatusEnum.待审核入职资料,
                            OfferWorkerStatusEnum.待签合同))
                    .le(TbOfferWorker::getPlanReportDate, DateUtil.offsetMonth(new Date(), -6))
                    .last("limit 1")
            );
            log.info("XXL-JOBtest, CancellationOfferWorker, 人数：{}", cancelPersons.size());
            for (TbOfferWorker cancelPerson : cancelPersons) {
                log.info("预备员工"+cancelPerson.getWorkerId()+"因超过计划入职时间6个月未入职，正在取消入职");
                try {
                    tbOfferWorkerService.approvalOfferWorkerData(cancelPerson.getWorkerId(), "超过计划入职时间6个月，自动取消入职",
                            4, "", BusiEnumDefine.NOTICE_MAIL_SEND_NO);
                    tbOfferWorkerHisService.moveWorkerToHisSingleForCancel(cancelPerson);
                } catch (Exception e) {
                    log.info("预备员工"+cancelPerson.getWorkerId()+"失败:"+e.getMessage());
                    log.error("预备员工"+cancelPerson.getWorkerId()+"失败:"+e.getMessage());
                }
            }
        } catch (Exception e) {
            log.info("XXL-JOB, CancellationOfferWorker失败:" + e.getMessage());
        }
    }
}
